package game;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;


import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class Game {
	private Collection<Object> collections;

	Method[] classMethods;

	// initialize field collection by given parameter from constructor and print
	// type of the collection
	public Game(Collection<Object> collections) {
		this.collections = collections;
		System.out.println(createMessage());
		printMethodNames();
	}

	// method return type of collection
	private String getTypeOfCollection() {
		return collections.getClass().getSimpleName();
	}

	// method create message for email to sent.Now we can use it to test our
	// methods
	private String createMessage() {
		String typeOfCollection = getTypeOfCollection();
		getDifferentObject();
		makeNewCollection();
		return typeOfCollection;
	}

	/** Returns the object which is different */
	private Object getDifferentObject() {
		if (collections.size() < 3 || collections == null)
			return "The collection should have 3 or more elements";

		Iterator<Object> iterator = collections.iterator();
		Object firstObject = iterator.next();
		Object secondObject = iterator.next();

		if (firstObject.getClass().equals(secondObject.getClass())) {
			while (iterator.hasNext()) {
				secondObject = iterator.next();
				if (!firstObject.getClass().equals(secondObject.getClass()))
					return secondObject;
			}
		} else {
			Object thirdObject = iterator.next();
			if (firstObject.getClass().equals(thirdObject.getClass()))
				return secondObject;
			else
				return firstObject;
		}
		return "";
	}

	/** Creates a new collection without the odd object */
	private void makeNewCollection() {
		Object oddObject = getDifferentObject();
		Iterator<Object> it = collections.iterator();
		while (it.hasNext()) {
			if (oddObject.getClass().equals(it.next().getClass()))
				it.remove();
		}
	}

	/**
	 * Prints the names of the methods in the objects that are in the collection
	 */
	public void printMethodNames() {
		Class reflectClass = collections.iterator().next().getClass();
		Method[] classMethods = reflectClass.getDeclaredMethods();

		for (Method method : classMethods)
			System.out.println(method.getReturnType().getSimpleName() + " "
					+ method.getName());
	}

	/**
	 * installing the javamail archive, set the settings, give emails for sender
	 * and recipient
	 */

	public void sendToEmail() {
		
		System.out.println("Serializing the collection");
		
		  try {
		      FileOutputStream fout = new FileOutputStream("serObject.txt");
		      ObjectOutputStream oos = new ObjectOutputStream(fout);
		      oos.writeObject(collections);
		      oos.close();
		      }
		   catch (Exception e) { e.printStackTrace(); }
		System.out.println("End serializing");

		
		
	    final String username = "ultimate.cool.yeah@gmail.com";
	    final String password = "ultimatecool123";

	    Properties props = new Properties();
	    props.put("mail.smtp.auth", true);
	    props.put("mail.smtp.starttls.enable", true);
	    props.put("mail.smtp.host", "smtp.gmail.com");
	    props.put("mail.smtp.port", "587");

	    Session session = Session.getInstance(props,
	            new javax.mail.Authenticator() {
	                protected PasswordAuthentication getPasswordAuthentication() {
	                    return new PasswordAuthentication(username, password);
	                }
	            });

	    try {

	        Message message = new MimeMessage(session);
	        message.setFrom(new InternetAddress("ultimate.cool.yeah@gmail.com"));
	        message.setRecipients(Message.RecipientType.TO,
	                InternetAddress.parse("l.nguyen189@gmail.com"));
	        message.setSubject("Testing Subject");
	        message.setText("PFA");

	        MimeBodyPart messageBodyPart = new MimeBodyPart();

	        Multipart multipart = new MimeMultipart();

	        messageBodyPart = new MimeBodyPart();
	        String file = System.getProperty("user.dir")+"\\serObject.txt";
	        String fileName = "serObject.txt";
	        DataSource source = new FileDataSource(file);
	        messageBodyPart.setDataHandler(new DataHandler(source));
	        messageBodyPart.setFileName(fileName);
	        multipart.addBodyPart(messageBodyPart);

	        message.setContent(multipart);

	        System.out.println("Sending");

	        Transport.send(message);

	        System.out.println("Done");

	    } catch (MessagingException e) {
	        e.printStackTrace();
	    }
	  }
	}
